﻿using System.ComponentModel;
using AilingKit.Shared.Authorization;
using AKit.Framework.Core.Identity.Tokens;
using AKit.Framework.Core.Identity.Tokens.Features.Generate;
using AKit.Framework.Core.Identity.Tokens.Features.Refresh;
using AKit.Framework.Core.Identity.Tokens.Models;
using AKit.Framework.Infrastructure.Exceptions;
using Infrastructure.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AilingKit.WebApi.Host.Controllers;


/// <summary>
/// 登录令牌
/// </summary>
[AllowAnonymous]
public class TokensController : BaseController
{
    private readonly ITokenService _service;
    private readonly IHttpContextAccessor _context;

    public TokensController(ITokenService service, IHttpContextAccessor context)
    {
        _service = service;
        _context = context;
    }

    /// <summary>
    /// 生成
    /// </summary>
    /// <param name="tenant"></param>
    /// <param name="command"></param>
    /// <returns></returns>
    [HttpPost]
    [Description("生成")]
    public async Task<TokenResponse> Generation([FromHeader(Name = TenantConstants.Identifier)] string tenant,TokenGenerationCommand command)
    {
        string ip = _context.HttpContext.GetIpAddress();
        return await _service.GenerateTokenAsync(command, ip);
    }

    /// <summary>
    /// 刷新
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    [HttpPost("refresh")]
    [Description("刷新")]
    public async Task<TokenResponse> Refresh(RefreshTokenCommand command)
    {
        string ip = _context.HttpContext.GetIpAddress();
        return await _service.RefreshTokenAsync(command, ip);
    }
}
